package org.hepeng.workx.spring.cloud.config;

import org.hepeng.workx.service.RestServiceCallResult;
import org.hepeng.workx.service.ServiceError;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository;
import org.springframework.cloud.config.server.environment.SearchPathCompositeEnvironmentRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author he peng
 */

@RestController
@RequestMapping(method = RequestMethod.GET, path = "${spring.cloud.config.server.prefix:}")
public class ConfigRepositoryController {

    private EnvironmentRepository repository;
    private AlterConfigRepositoryRequestParamHandler requestParamHandler;

    public ConfigRepositoryController(
            AlterConfigRepositoryRequestParamHandler requestParamHandler ,
            EnvironmentRepository repository) {
        this.requestParamHandler = requestParamHandler;
        this.repository = repository;
    }

//    @GetMapping("/git/repository")
    public ResponseEntity<RestServiceCallResult> getGitRepository() {
        JGitEnvironmentRepository gitRepository = JGitRepositoryUtils.getFirstRepository((SearchPathCompositeEnvironmentRepository) repository);
        return ResponseEntity.ok(RestServiceCallResult.newRestServiceCallResult(ServiceError.SUCCESS , gitRepository));
    }

    @PostMapping("/git/repository")
    public ResponseEntity<RestServiceCallResult> alterGitRepository(
            @RequestBody Map<String , Object> params , BindingResult error) {
        if (error.hasErrors()) {
            RestServiceCallResult callResult =
                    RestServiceCallResult.newRestServiceCallResult(ServiceError.PARAMETER_ERROR);
            return ResponseEntity.badRequest().body(callResult);
        }

        ResponseEntity<RestServiceCallResult> responseEntity;
        try {
            alterAndRefresh(params);
            RestServiceCallResult callResult = RestServiceCallResult.newRestServiceCallResult(ServiceError.SUCCESS);
            responseEntity = ResponseEntity.ok(callResult);
        } catch (Throwable t) {
            RestServiceCallResult callResult =
                    RestServiceCallResult.newRestServiceCallResult(
                            ServiceError.SERVICE_ERROR,
                            "alter git repository error , cause : " + t.getMessage());
            responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(callResult);
        }

        return responseEntity;
    }


    private void alterAndRefresh(Map<String , Object> params) {
        EnvironmentRepository repository = this.requestParamHandler.handle(this.repository, params);
        JGitEnvironmentRepository gitRepository = (JGitEnvironmentRepository) repository;
        gitRepository.refresh(gitRepository.getDefaultLabel());
    }
}
